package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.NaviGoodsService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.AllProductRequest;
import com.mall.shopping.dto.AllProductResponse;
import com.mall.shopping.dto.NavListResponse;
import com.mall.shopping.dto.RecommendResponse;
import com.mall.user.annotation.Anoymous;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;


/**
 * @author Alone
 * @version 1.0
 * @date 2020/7/10 15:58
 */
@Anoymous
@RestController
@RequestMapping("shopping")
public class NaviGoodsRecommendController {

    @Reference
    NaviGoodsService naviGoodsService;

    @Anoymous
    @GetMapping("/navigation")
    public ResponseData navigation() {
        NavListResponse response = naviGoodsService.naviList();
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil<>().setData(response.getPannelContentDtos());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @Anoymous
    @GetMapping("/goods")
    public ResponseData goods(
            @RequestParam(value = "cid",required = false) Long cid,
            @RequestParam(value = "page", defaultValue = "0", required = false) Integer page,
            @RequestParam(value = "size", defaultValue = "10", required = false) Integer size,
            @RequestParam(value = "sort", defaultValue = "0", required = false) String sort,
            @RequestParam(value = "priceGt") Integer priceGt,
            @RequestParam(value = "priceLte") Integer priceLte) {
        AllProductRequest request = new AllProductRequest();
        request.setCid(cid);
        request.setPage(page);
        request.setSize(size);
        request.setSort(sort);
        request.setPriceGt(priceGt);
        request.setPriceLte(priceLte);
        AllProductResponse response = naviGoodsService.queryGoods(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            //此处 data 和 productDtoList 不对应
            HashMap<String, Object> map = new HashMap<>();
            map.put("data", response.getProductDtoList());
            map.put("total", response.getTotal());
            return new ResponseUtil<>().setData(map);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @Anoymous
    @GetMapping("/recommend")
    public ResponseData recommend() {
        RecommendResponse response = naviGoodsService.queryRecommend();
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil<>().setData(response.getPanelContentItemDtos());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

}
